<%-- 
    Document   : profileEdit
    Created on : Apr 1, 2014, 6:22:05 PM
    Author     : asmaa
--%>

<%@include file="header.jsp" %>

<c:if test="${empty sessionScope.email || ( sessionScope.id != param.id && sessionScope.role == 0 )}">
    <c:redirect url="index.jsp"/>
</c:if>

<sql:query dataSource="${conn}" var="result">
    SELECT * from User where id=?
    <sql:param value="${param.id}" />
</sql:query>
    <script>
            function validateFormUser()
            {
                var name = document.forms["add"]["name"].value;
                var regexName = /^[a-zA-Z\s]+$/;
                if (!regexName.test(name))
                {
                    alert("Name should not contain special character");
                    document.add.name.focus();
                    return false;
                }
                var job = document.forms["add"]["job"].value;
                var regexJob = /^[a-zA-Z\s]+$/;
                if (!regexJob.test(job))
                {
                    alert("Job should not contain any numbers");
                    document.add.job.focus();
                    return false;
                }
                var interest = document.forms["add"]["interest"].value;
                var regexInt = /^[a-zA-Z\s\,]+$/;
                if (!regexInt.test(interest))
                {
                    alert("Interest should not contain any numbers");
                    document.add.interest.focus();
                    return false;
                }
                var address = document.forms["add"]["address"].value;
                var regexAdd = /^[a-zA-Z0-9\s\,]+$/;
                if (!regexAdd.test(address))
                {
                    alert("Address should not contain any special characters");
                    document.add.address.focus();
                    return false;
                }
               
                var pass = document.forms["add"]["password"].value;
                //alert("dd");
                var p = pass.length;
                //alert(p);
                if (pass == "" || pass.length < 6 || pass.length > 10)
                {
                    alert("Password must contain at least six characters and max 10");
                   // document.add.pass.focus();
                    return false;
                }
                
                //var email = document.forms["reg"]["email"].value;
                //var atpos = email.indexOf("@");
                //var dotpos = email.lastIndexOf(".");
                
                 var bdate = document.forms["add"]["bdate"].value;
                var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\-](0?[1-9]|1[012])[\-]\d{4}$/;
                var year = bdate.substring(bdate.lastIndexOf('-') + 1).toLowerCase();
                //alert(year);
                //alert("Year");
                if (!dateformat.test(bdate) || year > "1997")
                {
                    alert("Year of birth should be at least 1997 also format is dd-mm-yyyy");
                    document.add.bdate.focus();
                    return false;
                }
                /*if(regexName.test(name) && regexJob.test(job) && regexInt.test(interest)
                 &&  regexAdd.test(address) && regexEmail.test(email) &&Extension == "gif" || Extension == "png" || 
                 Extension == "bmp" || Extension == "jpeg" || Extension == "jpg" && pass != "" && pass.length >= 6 &&
                 pass.length <= 10 && regexCredit.test(credit) && regexCreditLim.test(creditLimit)
                 && dateformat.test(bdate)&& year <"1997")
                 {
                 
                 
                 }*/
                 return true;
            }
            function chkemail()
            {
                var email = document.forms["add"]["email"].value;
                var id=document.forms["add"]["id"].value;
                //var atpos = email.indexOf("@");
                //var dotpos = email.lastIndexOf(".");
                var regexEmail = /^\w+@[a-zA-Z_\.]+?\.[a-zA-Z]{2,3}$/;
                if (!regexEmail.test(email))
                {
                    alert("Not a valid e-mail address");
                    document.reg.email.focus();
                    return false;
                }
                var x;
                if(regexEmail.test(email))
                {
                    var reg = new XMLHttpRequest();
                    reg.open("GET", "CheckMail?email="+email+"&id="+id, true);
                    
                    reg.send(null);
                    reg.onreadystatechange = function() {
                        //alert("inside onreadystatechange");
                        if (reg.readyState == 4 && reg.status == 200) {
                            //alert(reg.responseText);
                            if (reg.responseText== 1 || reg.responseText=="1")
                            {
                                alert("Email Should be unique");
                                document.add.email.focus();
                                x=0;                                                                
                                //alert("x"+x);
                                //alert("x"+x);
                                return false;                                
                            }
                            else
                            {
                                //alert("x"+x);
                                x=1;
                            }
                        }
                                                   //return false;
                    }
                   // alert(x);
                    
                    //return false;
                    
                }
            }
            function chkall()
            {
                if(validateFormUser())
                {
                    //document.add.onsubmit()
                    return true;
                }else
                    return false;
            }
        </script>
<div class="top_bg">
    <div class="wrap">
        <div class="main_top">
            <h2 class="style">Profile</h2>
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col-md-12" style="margin-left: 60px;margin-top: 20px">
            <div class="panel panel-default">
                <div class="panel-body">
                    <c:forEach var="row" items="${result.rows}">
                        <form role="form"  name="add" method="post" action="UserProfileEdit">
                            <div class="form-group">
                                <input type="hidden" value="${row.id}" name="id">
                                <img src="DisplayUserImage?id=${row.id}" name="image" style="height:250px;weidth:300px;" alt="Responsive image"><br>
                            </div>
                            <div class="form-group">
                                <label for="name">Name</label><br>
                                <input type="text" name="name"  required value="${row.name}">
                            </div>
                            <div class="form-group">
                                <label for="job">Job</label><br>
                                <input name="job"  required value="${row.job}">
                            </div>
                            <div class="form-group">
                                <label for="address">Address</label><br>
                                <input type="text" required  name="address" value="${row.address}">
                            </div>
                            <div class="form-group">
                                <label for="password">Password</label><br>
                                <input type="text" required  name="password" value="${row.password}">
                            </div>
                            <div class="form-group">
                                <label for="email">Email</label><br>
                                <input type="text" onblur="chkemail()" required  name="email" value="${row.email}">
                            </div>
                            <div class="form-group">
                                <label for="interest">interest</label><br>
                                <input type="text" required  name="interest" value="${row.interest}">
                            </div>
                            <div class="form-group">
                                <label for="bdate">Birthday</label><br>
                                <input type="text" required  name="bdate" placeholder="dd-mm-yyyy" value="${row.birthday}">
                            </div>
                            <div class="form-group"> 
                                <button type="submit" class="btn btn-success" style="padding-top: 10px" onclick="return chkall()">Save</button>
                            </div>
                        </c:forEach>   
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="footer.jsp" %>